package com.bw.skill.mvvm.viewmodel;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.mvvm_common.dao.UserBean;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.field.UIField;
import com.bw.mvvm_common.mvvm.utils.GreenDaoUtils;

import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.skill.R;
import com.bw.skill.SeckillAction;
import com.bw.skill.TimeServer;
import com.bw.skill.adapter.SeckillAdapter;
import com.bw.skill.entity.AddSeckillEntity;
import com.bw.skill.entity.SeckillEntity;
import com.bw.skill.mvvm.model.SkillModel;
import com.bw.skill.mvvm.view.SeckillvaluesActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import org.greenrobot.eventbus.EventBus;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public
/**
 * @author Lishaohe
 * @description:
 * @date :2021/10/26 0026 下午 3:52
 *
 */
class SkillViewModel extends BaseViewModel<SkillModel> implements AdapterView.OnItemClickListener {
    public ObservableField<String> starttimee = new ObservableField<>();
    public ObservableField<String> endtimee = new ObservableField<>();
    public static ObservableField<String> nowtime = new ObservableField<>();
    public BaseQuickAdapter adapter = new SeckillAdapter();
    List<SeckillEntity.DataBean> data;
    int endtime;
    int starttime;
    public static Bundle bundle;
    HashMap<String,Object> map1;

    @Override
    public void onCreate() {
        super.onCreate();

        request();

    }
    public void request(){
        HashMap<String,Object> map = new HashMap<>();
        m.requestSeckill(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(  BaseQuickAdapter<?, ?> adapter,   View view, int position) {
                endtime = data.get(position).getEndtime();
                starttime = data.get(position).getStarttime();

                bundle = new Bundle();
                bundle.putString("icon",data.get(position).getGoods_default_icon());
                bundle.putString("sku",data.get(position).getGoods_default_sku());
                bundle.putString("desc",data.get(position).getGoods_desc());
                bundle.putString("price", data.get(position).getGoods_default_price());
                bundle.putInt("id",data.get(position).getId());
                bundle.putInt("count",data.get(position).getGoods_num());

                HashMap<String ,Object> map = new HashMap<>();
                map.put(UIField.PAGECLAZZ, SeckillvaluesActivity.class);
                map.put(UIField.PAGEBUNDLE,bundle);
                startPage(map);
                TextView tv = view.findViewById(R.id.seckill_add_tv);
                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        List<UserBean> list = GreenDaoUtils.getGreenDaoUtils().green().queryBuilder().list();
                        if (list.isEmpty()){
                            ToastUtils.showLong("没登录");
                        }else {
                            int user_id = list.get(0).getUser_id();
                            map1 = new HashMap<>();
                            map1.put("goodsCount",data.get(position).getGoods_num());
                            map1.put("goodsDesc",data.get(position).getGoods_desc());
                            map1.put("goodsIcon",data.get(position).getGoods_default_icon());
                            map1.put("goodsId",data.get(position).getId());
                            map1.put("goodsPrice",data.get(position).getGoods_default_price());
                            map1.put("goodsSku",data.get(position).getGoods_default_sku());
                            map1.put("userId",user_id);

                            SeckillAction seckillAction = new SeckillAction();
                            seckillAction.action="SkillListGetNowTime";
                            EventBus.getDefault().post(seckillAction);

                        }

                    }
                });
            }
        });
    }
    public void requestSkill(long nowTime){


        map1.put("time",nowTime);
        m.requestGoodsSeckill(map1)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);

    }
    @Override
    protected void result(BaseEntity entity) {
        if (entity instanceof SeckillEntity) {
            SeckillEntity seckillEntity = (SeckillEntity) entity;
            data = seckillEntity.getData();

            adapter.setNewInstance(data);

            int starttime = data.get(0).getStarttime();
            long start = starttime;
            Date date = TimeUtils.millis2Date(start*1000);
            String s = TimeUtils.date2String(date);

            int endtime = data.get(0).getEndtime();
            long end = endtime;
            Date date1 = TimeUtils.millis2Date(end*1000);
            String s1 = TimeUtils.date2String(date1);

            starttimee.set("开始时间："+s);
            endtimee.set("结束时间："+s1);

            SeckillAction seckillAction = new SeckillAction();
            seckillAction.action = "SkillListActivity";
            seckillAction.nowTime = data.get(0).getNowtime();
            EventBus.getDefault().post(seckillAction);

        }else if (entity instanceof AddSeckillEntity){
                AddSeckillEntity addSeckillEntity = (AddSeckillEntity) entity;
                 if (addSeckillEntity.getMessage().equals("添加购物车成功")) {
                    ToastUtils.showLong("抢购成功");
            }

        }
    }



    //返回
    public void finish(){
        finishPage();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
